{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.输入某年某月某日，判断这一天是这一年的第几天？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023年2月3日是这一年的第34天\n"
     ]
    }
   ],
   "source": [
    "import datetime\n",
    "def day_of_year(year, month, day):\n",
    "    date = datetime.date(year, month, day)\n",
    "    day_number = date.timetuple().tm_yday\n",
    "    return day_number\n",
    "year = int(input(\"请输入年份: \"))\n",
    "month = int(input(\"请输入月份: \"))\n",
    "day = int(input(\"请输入日期: \"))\n",
    "print(f\"{year}年{month}月{day}日是这一年的第{day_of_year(year, month, day)}天\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.判断1000中有多少个素数，并输出所有素数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "在1到1000之间有 168 个素数。\n",
      "所有素数为：\n",
      "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
     ]
    }
   ],
   "source": [
    "def sieve_of_eratosthenes(limit):\n",
    "    is_prime = [True] * (limit + 1)\n",
    "    p = 2\n",
    "    while (p * p <= limit):\n",
    "        if (is_prime[p] == True):\n",
    "            for i in range(p * p, limit + 1, p):\n",
    "                is_prime[i] = False\n",
    "        p += 1\n",
    "    prime_numbers = [p for p in range(2, limit + 1) if is_prime[p]]\n",
    "    return prime_numbers\n",
    "limit = 1000\n",
    "prime_numbers = sieve_of_eratosthenes(limit)\n",
    "print(f\"在1到{limit}之间有 {len(prime_numbers)} 个素数。\")\n",
    "print(\"所有素数为：\")\n",
    "print(prime_numbers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.利用条件运算符的嵌套来完成此题：学习成绩>=90分的同学用A表示，60-89分之间的用B表示，60分以下的用C表示。（要求有键盘输入）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该同学的成绩等级为：A\n"
     ]
    }
   ],
   "source": [
    "score = float(input(\"请输入学习成绩：\"))\n",
    "grade = 'A' if score >= 90 else('B' if 60 <= score < 90 else 'C')\n",
    "print(f\"该同学的成绩等级为：{grade}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "7.输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "英文字母个数：5\n",
      "空格个数：0\n",
      "数字个数：4\n",
      "其他字符个数：0\n"
     ]
    }
   ],
   "source": [
    "input_string = input(\"请输入一行字符串：\")\n",
    "letters_count = 0\n",
    "spaces_count = 0\n",
    "digits_count = 0\n",
    "others_count = 0\n",
    "for char in input_string:\n",
    "    if char.isalpha():\n",
    "        letters_count += 1\n",
    "    elif char.isspace():\n",
    "        spaces_count += 1\n",
    "    elif char.isdigit():\n",
    "        digits_count += 1\n",
    "    else:\n",
    "        others_count += 1\n",
    "print(f\"英文字母个数：{letters_count}\")\n",
    "print(f\"空格个数：{spaces_count}\")\n",
    "print(f\"数字个数：{digits_count}\")\n",
    "print(f\"其他字符个数：{others_count}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "9.一个数如果恰好等于它的因子之和，这个数就称为“完数”。例如6=1＋2＋3.编程，找出1000以内的所有完数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000以内的所有完数有：\n",
      "[6, 28, 496]\n"
     ]
    }
   ],
   "source": [
    "def find_factors(n):\n",
    "    \"\"\"找出n的所有因子（不包括n本身）\"\"\"\n",
    "    factors = []\n",
    "    for i in range(1, n):\n",
    "        if n % i == 0:\n",
    "            factors.append(i)\n",
    "    return factors\n",
    "\n",
    "def is_perfect_number(n):\n",
    "    \"\"\"判断n是否为完数\"\"\"\n",
    "    factors = find_factors(n)\n",
    "    return sum(factors) == n\n",
    "perfect_numbers = []\n",
    "for num in range(1, 1001):\n",
    "    if is_perfect_number(num):\n",
    "        perfect_numbers.append(num)\n",
    "print(\"1000以内的所有完数有：\")\n",
    "print(perfect_numbers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "11.输入任意数N，求1+2!+3!+...+N!的值（注意实数范围）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 + 2! + 3! + ... + 3! = 9\n"
     ]
    }
   ],
   "source": [
    "def factorial(n):\n",
    "    \"\"\"计算n的阶乘\"\"\"\n",
    "    if n == 0 or n == 1:\n",
    "        return 1\n",
    "    else:\n",
    "        result = 1\n",
    "        for i in range(2, n + 1):\n",
    "            result *= i\n",
    "        return result\n",
    "def sum_of_factorials(N):\n",
    "    \"\"\"计算1 + 2! + 3! + ... + N!的值\"\"\"\n",
    "    total = 0\n",
    "    for num in range(1, N + 1):\n",
    "        total += factorial(num)\n",
    "    return total\n",
    "N = int(input(\"请输入一个整数N：\"))\n",
    "result = sum_of_factorials(N)\n",
    "print(f\"1 + 2! + 3! + ... + {N}! = {result}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "13.输入任意奇数N，按如下要求打印出图案（菱形）\n",
    "    *\n",
    "  ***\n",
    " *****\n",
    "*******（N控制行数和本行的星数）\n",
    " *****\n",
    "  ***\n",
    "   *\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  *\n",
      " ***\n",
      "*****\n",
      " ***\n",
      "  *\n"
     ]
    }
   ],
   "source": [
    "def print_diamond(N):\n",
    "    if N % 2 == 0:\n",
    "        print(\"请输入一个奇数N。\")\n",
    "        return\n",
    "    half_height = (N + 1) // 2\n",
    "    for i in range(1, half_height + 1):\n",
    "        spaces = ' ' * (half_height - i)\n",
    "        stars = '*' * (2 * i - 1)\n",
    "        print(spaces + stars)\n",
    "    for i in range(half_height - 1, 0, -1):\n",
    "        spaces = ' ' * (half_height - i)\n",
    "        stars = '*' * (2 * i - 1)\n",
    "        print(spaces + stars)\n",
    "N = int(input(\"请输入一个奇数N：\"))\n",
    "print_diamond(N)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "15.对10个数进行排序。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入10个数：\n",
      "排序后的数字是：\n",
      "[2, 2, 13, 22, 22, 23, 43, 45, 67, 84]\n"
     ]
    }
   ],
   "source": [
    "numbers = []\n",
    "print(\"请输入10个数：\")\n",
    "for i in range(10):\n",
    "    while True:\n",
    "        try:\n",
    "            num = int(input(f\"请输入第{i+1}个数：\"))\n",
    "            numbers.append(num)\n",
    "            break  \n",
    "        except ValueError:\n",
    "            print(\"输入无效，请输入一个整数。\")\n",
    "numbers.sort()\n",
    "print(\"排序后的数字是：\")\n",
    "print(numbers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "17.有一个已经排好序的数组（元素数量大于10）。从键盘任意输入一个数，要求按原来的规律将它插入数组中。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "插入后的数组是：\n",
      "[6, 9, 12, 15, 18, 21, 24, 27, 36, 234, 666]\n"
     ]
    }
   ],
   "source": [
    "sorted_array = [6, 9, 12, 15, 18, 21, 24, 27, 36, 666]\n",
    "number_to_insert = int(input(\"请输入一个要插入的数：\"))\n",
    "insert_position = 0\n",
    "while insert_position < len(sorted_array) and sorted_array[insert_position] < number_to_insert:\n",
    "    insert_position += 1\n",
    "sorted_array.insert(insert_position, number_to_insert)\n",
    "print(\"插入后的数组是：\")\n",
    "print(sorted_array)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "19.有n个人围成一圈，顺序排号。从第一个人开始报数（从1到3报数），凡报到3的人退出圈子，问最后留下的是原来第几号的那位。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最后留下的是原来第8号的那位。\n"
     ]
    }
   ],
   "source": [
    "def josephus_problem(n, k):\n",
    "    \"\"\"\n",
    "    解决约瑟夫环问题。\n",
    "    \n",
    "    参数:\n",
    "    n -- 总人数\n",
    "    k -- 报数的阈值（报到k的人退出）\n",
    "    \n",
    "    返回:\n",
    "    最后留下的人的编号（从1开始计数）\n",
    "    \"\"\"\n",
    "    people = list(range(1, n + 1))\n",
    "    index = 0\n",
    "    while len(people) > 1:\n",
    "        index = (index + k - 1) % len(people)\n",
    "        people.pop(index)\n",
    "    return people[0]\n",
    "n = int(input(\"请输入总人数n：\"))\n",
    "k = 3\n",
    "result = josephus_problem(n, k)\n",
    "print(f\"最后留下的是原来第{result}号的那位。\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
